package com.usian.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.ProvincesMapper;
import com.usian.mapper.XiansuoMapper;
import com.usian.model.dtos.XiansuoDto;
import com.usian.model.pojos.Provinces;
import com.usian.model.pojos.Xiansuo;
import com.usian.model.pojos.XiansuoResult;
import com.usian.service.XiansuoService;
import com.usian.utils.AppHttpCodeEnum;
import com.usian.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XiansuoServiceImpl extends ServiceImpl<XiansuoMapper, Xiansuo> implements XiansuoService {

    @Autowired
    private XiansuoMapper xiansuoMapper;

    @Autowired
    private ProvincesMapper provincesMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseResult xiansuoList(XiansuoDto dto) {

        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        PageHelper.startPage(dto.getPageNum(),dto.getSize());

        List<Xiansuo> xiansuoList = xiansuoMapper.findXiansuo(dto);

        PageInfo<Xiansuo> pageInfo =new PageInfo<>(xiansuoList);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public List<Provinces> findPro(Integer parentId, Integer rank) {

        if(parentId==null || rank==null){
            return null;
        }

        //判断等级
        QueryWrapper<Provinces> queryWrapper =new QueryWrapper<>();
        if(rank==1){
            queryWrapper.lambda().eq(Provinces::getPRank,1);
        }else{
            queryWrapper.lambda().eq(Provinces::getPRank,rank);
            queryWrapper.lambda().eq(Provinces::getIsParent,parentId);
        }
        List<Provinces> provincesList = provincesMapper.selectList(queryWrapper);
        return provincesList;
    }

    @Override
    public ResponseResult saveXiansuo(Xiansuo xiansuo) {

        boolean save = this.save(xiansuo);

        //String
        redisTemplate.boundValueOps("suoxian").set(JSONObject.toJSONString(xiansuo));

        if(save){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult shenhe(Xiansuo xiansuo) {
        if(xiansuo==null || (xiansuo.getId()==null ||xiansuo.getStatus()==null)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        boolean b = this.updateById(xiansuo);
        if(b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ResponseResult banjie(XiansuoResult xiansuoResult) {

        int a =1;
        //用rabbtMQ发送消息


        return null;
    }
}
